package com.wnxy.jdk8;

import com.wnxy.jdk8.entity.Author;
import com.wnxy.jdk8.entity.Book;

import java.sql.SQLOutput;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class App4 {
    private static List<Author> authors = getAuthors();
    public static void main(String[] args) throws Throwable {
        t23();
    }

    private static void t23() {

        Stream.iterate(0L, n -> n+1).limit(10_000_000L).parallel()
                //.peek(c-> System.out.println(Thread.currentThread().getName()))
                .reduce((result,ele)->result+ele)
                .ifPresent(c-> System.out.println("c = " + c));
    }

    private static void t22() {
        authors.stream()
                .distinct()
                .mapToInt(a->a.getAge())
                .map(num -> num+20)
                .filter(value -> value>30)
                .forEach(System.out::println);
    }

    private static void t21() {
        authors.stream()
                .map(author -> author.getName())
                .map(StringBuilder::new)
                .map(sb->sb.append("-jery").toString())
                .forEach(str-> System.out.println(str));
    }

    private static void t20() {
        StringBuilder sb = new StringBuilder();
        authors.stream()
                .map(author -> author.getName())
                .forEach(sb::append);
    }

    private static void t19() {
        authors.stream()
                .distinct()
                .map(author -> author.getAge())
                .map(String::valueOf)
                .forEach(System.out::println);
    }

    private static void t18() {
        //需求：我们想获取作家的书籍集合。
        Author author = authors.get(0);
        Optional.ofNullable(author)
                .map(a->a.getBooks())
                .ifPresent(list->list.forEach(System.out::println));
    }

    private static void t17() {
        Author author = authors.get(0);
        Optional<Author> optional = Optional.ofNullable(author);
        optional.filter(a->a.getAge()>180)
                .ifPresent(a-> System.out.println(a));
    }

    private static void t16() throws Throwable {
        Author author = null;//authors.get(0);
        Optional<Author> optional = Optional.ofNullable(author);
        Author author1 = optional.orElseThrow(() -> new RuntimeException("error"));
        System.out.println("author1 = " + author1);
    }

    private static void t15() {
        Author author = authors.get(0);
        Optional<Author> optional = Optional.ofNullable(author);
        Author author1 = optional.orElseGet(Author::new);
        System.out.println("author1 = " + author1);
    }

    private static void t13() {
        //Optional<List<Author>> optional = Optional.ofNullable(getAuthors());
        //List<Author> authors = optional.get();
        //optional.ifPresent(authors -> authors.stream().distinct().forEach(System.out::println));

        Author author = null;//getAuthors().get(0);
        Optional<Author> op = Optional.ofNullable(author);
        Author author2 = op.orElse(new Author());
        System.out.println("author2 = " + author2);
    }

    private static void t12() {
        //使用reduce求所有作者年龄的和
        Integer sum = authors.stream()
                .distinct()
                .map(a -> a.getAge())
                .reduce(0, (n1, n2) -> n1 + n2);
        System.out.println("sum = " + sum);

        Optional<Integer> reduce = authors.stream()
                .distinct()
                .map(a -> a.getAge())
                .reduce((n1, n2) -> n1 + n2);
        reduce.ifPresent(System.out::println);
    }

    private static void t11() {
        //获取任意一个年龄大于18的作家，如果存在就输出他的名字
        Optional<Author> optional = authors.stream()
                .filter(a->a.getAge()>180)
                .findAny();
        optional.ifPresent(author -> System.out.println("author = " + author));

    }

    private static void t10() {
        //判断是否有年龄在29以上的作家
        boolean b = authors.stream()
                .anyMatch(a -> a.getAge() > 29);
        System.out.println("b = " + b);
    }

    private static void t09() {
        //获取一个存放所有作者名字的List集合。
        List<Author> collect = authors.stream().collect(Collectors.toList());

        List<String> collect1 = authors.stream().map(a -> a.getName()).collect(Collectors.toList());

        Set<String> collect2 = authors.stream().map(a -> a.getName()).collect(Collectors.toSet());

        // 获取一个Map集合，map的key为作者名，value为List<Book>
        Map<String, List<Book>> collect3 = authors.stream()
                .distinct()
                .collect(Collectors.toMap(a->a.getName(), a->a.getBooks()));
        System.out.println("collect3 = " + collect3);
    }

    private static void t08() {
        //分别获取这些作家的所出书籍的最高分并打印。
        Optional<Integer> optional = authors.stream()
                .flatMap(a -> a.getBooks().stream())
                .map(b -> b.getScore())
                .max((b1, b2) -> b1 - b2);
        Integer max = optional.get();
        System.out.println("max = " + max);
    }

    private static void t07() {
        //打印这些作家的所出书籍的数目，注意删除重复元素。
        long count = authors.stream()
                .flatMap(a -> a.getBooks().stream())
                .distinct()
                .count();
        System.out.println("count = " + count);
    }

    private static void t06() {
        //打印所有的图书分类。要求对分类进行去重。不能出现这种格式：哲学,爱情
        authors.stream()
                .distinct()
                .flatMap(a->a.getBooks().stream())
                .flatMap(b->Stream.of(b.getCategory().split(",")))
                .distinct()
                .forEach(System.out::println);
    }

    private static void t05() {
        //打印所有书籍的名字。要求对重复的元素进行去重。
        authors.stream()
                .distinct()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .forEach(System.out::println);
    }

    private static void t04() {
        //对流中的元素按照年龄进行降序排序，并且要求不能有重复的元素。
        authors.stream()
                .sorted((o1, o2)->o2.getAge() - o1.getAge())
                .forEach(System.out::println);
    }

    private static void t3() {
        // 打印所有姓名长度大于1的作家的姓名
        authors.stream()
                .filter(author -> author.getName().length()>1)
                .map(author -> author.getName())
                .forEach(System.out::println);
    }

    private static void t2() {
        Map<String,Integer> map = new HashMap<>();
        map.put("蜡笔小新",19);
        map.put("黑子",17);
        map.put("日向翔阳",16);
        Set<Map.Entry<String, Integer>> set = map.entrySet();
        set.stream()
                .filter(entry -> entry.getValue()>18)
                .forEach(entry -> System.out.println(entry.getKey()+"="+entry.getValue()));
    }

    private static void t1() {
        //需求：打印所有年龄小于18的作家的名字，并去重。
        List<Author> authors = getAuthors();
        authors.stream()
                .filter(author -> author.getAge() < 18)
                .map(author -> author.getName())
                .distinct()
                .forEach(System.out::println);


    }

    private static List<Author> getAuthors() {
        //数据初始化
        Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);
        Author author2 = new Author(2L,"亚拉索",15,"狂风也追逐不上他的思考速度",null);
        Author author3 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);
        Author author4 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);

        //书籍列表
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();

        books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));
        books1.add(new Book(2L,"一个人不能死在同一把刀下","个人成长,爱情",99,"讲述如何从失败中明悟真理"));

        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(4L,"吹或不吹","爱情,个人传记",56,"一个哲学家的恋爱观注定很难把他所在的时代理解"));

        books3.add(new Book(5L,"你的剑就是我的剑","爱情",56,"无法想象一个武者能对他的伴侣这么的宽容"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));

        author.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);

        List<Author> authorList = new ArrayList<>(Arrays.asList(author,author2,author3,author4));
        return authorList;
    }
}